import { Table } from 'antd'
import { useState } from 'react'

import AekDrawer, { PromiseThen } from '@/components/AekDrawer'
import { getRowSelection, message } from '@/utils/antdHelper'

const SelFieldsDrawer = ({ tableFullInfo, selList: oldSelList }) => {
  const [selList, setSelList] = useState(oldSelList || [])

  /** @type {import("@/components/AekTable").AekTableProps} */
  const tableProps = {
    configKey: 'addNewPage',
    rowKey: 'COLUMN_NAME',
    dataSource: tableFullInfo,
    pagination: false,
    columns: [
      { dataIndex: 'COLUMN_NAME', title: '字段名' },
      { dataIndex: 'COLUMN_TYPE', title: '字段类型' },
      { dataIndex: 'IS_NULLABLE', title: '是否允许为空' },
      { dataIndex: 'COLUMN_DEFAULT', title: '默认值' },
      { dataIndex: 'COLUMN_COMMENT', title: '字段描述' },
    ],
    rowSelection: getRowSelection({ selList, setSelList, rowKey: 'COLUMN_NAME' }),
  }

  const onOk = async () => {
    if (!selList.length) {
      message.warning('请先选择字段')
      return Promise.reject()
    }
    return selList
  }

  return (
    <AekDrawer title='选择字段' width={800} onOk={onOk}>
      <Table {...tableProps} />
    </AekDrawer>
  )
}

SelFieldsDrawer.PromiseThen = PromiseThen

export default SelFieldsDrawer
